//
//  RVMyAddressController.swift
//  AirTravel
//
//  Created by apple on 2021/11/24.
//

import UIKit
import HandyJSON

class RVMyAddressController: CommonVC {

    @IBOutlet weak var tableView: UITableView!
    
    var dataSource: [RVAddressList] = []
    
    @IBOutlet weak var confirmBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        head.title.text = "我的地址"
        confUI()
    }

    func confUI(){
        confirmBtn.layer.borderWidth = 1
        confirmBtn.layer.borderColor = UIColor(hexString: "FF8A00").cgColor
        confirmBtn.setCorner(radius: 1.5)

        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 100
        tableView.register(UINib(nibName: "RVAddressListCell", bundle: nil), forCellReuseIdentifier: "address_list")
        getMyAddress()
    }
    
    @IBAction func clickConfirmBtn(_ sender: Any) {
        
        let vc = RVNewddressController()
        navigationController?.pushViewController(vc, animated: true)
        
    }
    
    
    func getMyAddress() {
        
        YProgressHUD.show()
        NetWorkRequest(API1.getMyAddress) { json in
            YProgressHUD.dismiss()
            guard let data = JSONDeserializer<RVMyAddressData>.deserializeFrom(dict: json.dictionaryObject) else{
                return
            }
            self.dataSource = data.data
            self.tableView.reloadData()
        } failed: { msg in
            YProgressHUD.dismiss()

        } errorResult: {
            YProgressHUD.dismiss()
        }
        
        
    }

   

}
extension RVMyAddressController: UITableViewDelegate, UITableViewDataSource{
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "address_list", for: indexPath) as! RVAddressListCell
        let list = dataSource[indexPath.row]
        cell.titleLabel.text = list.name + "   " + list.mobile;
        cell.addressLabel.text = list.full_address;
        if list.is_default == "0" {
            cell.labelLeading.constant = 0
            cell.labelWidth.constant = 0
        }else{
            cell.labelLeading.constant = 8
            cell.labelWidth.constant = 29
        }
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}
